import updateView from './updateView.js'
import store from '@/store/index';
export {
	checkVersion
}

function checkVersion() { 
	updateState(true)
	let versionLocal = null;
	plus.runtime.getProperty(plus.runtime.appid, function(info) { 
		versionLocal = info.version

		//检测升级
		uni.request({ 
			url: 'https://nfttest.hiyanai.com/nftshop/hotfix/version.txt?v=' +
				new Date().getTime(), //检查更新的服务器地址
			method: 'GET',
			success: (res) => {
				if (res.statusCode && res.statusCode == 200) { // api错误
					let item = null
					let info = {}

					if (uni.getSystemInfoSync().platform == 'android') {
						info = res.data.android
					} else if (uni.getSystemInfoSync().platform == 'ios') {
						info = res.data.ios
					} 
					for (let index = 0; index < info.versions.length; index++) {
						if (info.versions[index].version == versionLocal) {
							item = info.versions[index];
							break;
						}
					}
					if (item) {
						//processing = false;
						updateState(false)
						console.log("你已经是最新版本..")
					} else {
						//先判断大版本
						if (versonHigh(versionLocal) != versonHigh(info.versions[0].version)) {
							console.log("---------->大版本不同")
							// 更新软件
							if (uni.getSystemInfoSync().platform == 'android') {
								installAndroid(info)
							} else if (uni.getSystemInfoSync().platform == 'ios') {
								uni.showModal({
									title: '新版本提示',
									content: '发现新的版本 点击确定更新！',
									showCancel: false,
									success: function(res) {
										plus.runtime.openURL(info.downlaodurl);
									}
								})
							}
							//判断小版本
						} else if (versonLow(versionLocal) != versonLow(info.versions[0].version)) {
							console.log("---------->小版本不同")
							// 更新资源 
							installAssets(info.versions[0])
						}
					}
				}
			},
			fail: (res) => {
				console.log("updateVersion Fail")
				updateState(false)
				// processing = false;
			}
		})
	})


}
//由于 IOS 版本只能三段 1.1.1
//大版本
function versonHigh(vers) {
	let arrayVers = vers.split('.')
	let versHigh = ''
	if (arrayVers.length == 3) {
		versHigh = arrayVers[0] + '.' + arrayVers[1]
	} else {
		//无效的版本信息
		console.log('获取大版本出错')
	}
	return versHigh
}

//小版本
function versonLow(vers) {
	let arrayVers = vers.split('.')
	let versLow = ''
	if (arrayVers.length == 3) {
		versLow = arrayVers[2]
	} else {
		//无效的版本信息
		console.log('获取小版本出错')
		updateState(false)
	}
	return versLow
}

function installAndroid(info) {
	updateView.viewHotFixInit();
	uni.downloadFile({
		url: info.downlaodurl,
		complete: (res) => {
			updateView.viewHotFixHide()
			if (res && res.statusCode === 200) {
				plus.runtime.install(plus.io.convertLocalFileSystemURL(res.tempFilePath), {}, {}, function(
					error) {
					uni.showToast({
						title: '安装失败',
						icon: 'none',
						position: 'bottom',
						duration: 2000,
					});
					//processing = false;
					updateState(false)
				})
			} else {
				uni.showToast({
					title: '安装包下载失败',
					icon: 'none',
					position: 'bottom',
					duration: 2000,
				});
				// processing = false;
				updateState(false)
			}
		}
	}).onProgressUpdate((res) => {
		//console.log('下载进度' + res.progress);
		updateView.viewHotFixUpdate(res.progress)
	})
}

function installAssets(info) {
	updateView.viewHotFixInit();
	uni.downloadFile({
		url: info.assets,
		complete: (res) => {
			//console.log("downloadFile: complete: (res)", res)
			updateView.viewHotFixHide()
			if (res && res.statusCode === 200) {
				plus.runtime.install(res.tempFilePath, {}, function(wgtinfo) {
					plus.nativeUI.alert("更新完成，需重启应用！", function() {
						plus.runtime.restart()
					});
				}, function(error) {
					uni.showToast({
						title: error ? error.message : '热刷新失败',
						icon: 'none',
						position: 'bottom',
						duration: 2000,
					});
					//processing = false;
					updateState(false)
				});
			} else {
				uni.showToast({
					title: '资源下载失败',
					icon: 'none',
					position: 'bottom',
					duration: 2000,
				});
				//processing = false;
				updateState(false)
			}


		}
	}).onProgressUpdate((res) => {
		// this.processText = res.progress
		//console.log('下载进度' + res.progress);
		updateView.viewHotFixUpdate(res.progress)
	})

}

function updateState(value) {
	// console.log("updateState:", value)
	if (store) {
		store.commit('indexCore/setCheckState', value)
	}
}
